package com.hdl.util;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送邮件工具类
 */
public class MailUtil {

    /**
     * 给需要激活的用户发送激活码邮件
     * @param emailAccount 接收这的账号
     * @param activeCode 激活码
     * @throws MessagingException
     */
    public static void send(String emailAccount, String activeCode) throws MessagingException {

        //0.1 服务器的配置
        Properties pro = new Properties();
        /*pro.setProperty("mail.host","smtp.qq.com");//给qq发送邮件*/

        //0.2 账号和密码
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                //设置发送者的邮箱账户和密码
                return new PasswordAuthentication("admin@store.com","admin");
            }
        };

        //1 与服务器建立连接 Session
        Session session = Session.getDefaultInstance(pro, authenticator);

        //2 编写邮件
        Message message = new MimeMessage(session);

        //2.1 发件人(模拟账号)
        message.setFrom(new InternetAddress("admin@store.com"));

        //2.2 收件人 to:收件人 cc:抄送 bcc:暗送(密送) (模拟账号)
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailAccount));

        //2.3 主题
        message.setSubject("用户激活");//用户激活

        //2.4 内容
        String url = "http://localhost:8080/hdl/activeUserServlet?activeCode="+activeCode;
        String content = "<h1>来自嗨底捞的激活邮件!激活请点击以下链接!</h1><h3><a href='"+url+"'>"+url+"</a></h3>";
        message.setContent(content,"text/html;charset=utf-8");

        //3 发送消息 Transport
        /*Transport transport = session.getTransport();
        transport.connect("1779686364@qq.com","eukinzzzvzwaebad");
        transport.sendMessage(message,message.getAllRecipients());*/
        Transport.send(message);

    }


    /**
     * 可以给QQ用户他送邮件
     * @param emailAccount
     * @param activeCode
     * @throws MessagingException
     */
    public static void sendQQ(String emailAccount, String activeCode) throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");// 连接协议
        properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
        properties.put("mail.smtp.port", 465);// 端口号
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接 ---一般都使用
        properties.put("mail.debug", "true");//设置是否显示debug信息 true 会在控制台显示相关信息
        //得到回话对象
        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        //设置发件人邮箱地址
        message.setFrom(new InternetAddress("1779686364@qq.com"));
        //设置收件人地址
        message.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress[]{new InternetAddress(emailAccount)});
        //设置邮件标题
        message.setSubject("用户激活");
        //设置邮件内容
        String url = "http://localhost:8080/hdl/activeUserServlet?activeCode="+activeCode;
        String content = "<h1>来自嗨底捞的激活邮件!激活请点击以下链接!</h1><h3><a href='"+url+"'>"+url+"</a></h3>";
        message.setContent(content,"text/html;charset=utf-8");
        message.setText(content);
        //得到邮差对象
        Transport transport = session.getTransport();
        //连接自己的邮箱账户
        transport.connect("1779686364@qq.com", "eukinzzzvzwaebad");//密码为刚才得到的授权码
        transport.sendMessage(message, message.getAllRecipients());
    }
    
    /**
     * 可以给QQ用户他送邮件
     * @param emailAccount
     * @param activeCode
     * @throws MessagingException
     */
    public static void sendPsd(String emailAccount, String psd) throws MessagingException {
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");// 连接协议
        properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
        properties.put("mail.smtp.port", 465);// 端口号
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接 ---一般都使用
        properties.put("mail.debug", "true");//设置是否显示debug信息 true 会在控制台显示相关信息
        //得到回话对象
        Session session = Session.getInstance(properties);
        // 获取邮件对象
        Message message = new MimeMessage(session);
        //设置发件人邮箱地址
        message.setFrom(new InternetAddress("1779686364@qq.com"));
        //设置收件人地址
        message.setRecipients(MimeMessage.RecipientType.TO, new InternetAddress[]{new InternetAddress(emailAccount)});
        //设置邮件标题
        message.setSubject("密码找回");
        //设置邮件内容
        String content = "<h1>尊敬的用户，您的密码为"+psd+",请牢记，切莫遗忘!</h1>";
        message.setContent(content,"text/html;charset=utf-8");
        message.setText(content);
        //得到邮差对象
        Transport transport = session.getTransport();
        //连接自己的邮箱账户
        transport.connect("1779686364@qq.com", "eukinzzzvzwaebad");//密码为刚才得到的授权码
        transport.sendMessage(message, message.getAllRecipients());
    }
}
